/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package RunableClass;


import RunableClass.useClass.Addport;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.URL;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import mysql.connect;
import net.ipaddress.authenticator;

/**
 *
 * @author Anatomy
 */
public class GroupRunnable implements Runnable{
    private JTable JT;
  //  private int i;
    private int index;

    public GroupRunnable(JTable JT) {
        this.JT = JT;
    }
   int count=1;int count1=0;
      static  StringBuffer     content        =   new StringBuffer(""); 
    @Override
    public void run() {
        for(;;){
   //////////////////////////////////////////////////////////////////////////
              //////////////////////////////////สร้างการดึงค่าจาก URL/////////////////////////////////////////////////////////////////////////////**/
     /**/         try {
         
               content = new StringBuffer("");int count2=1;
         String buffer;
        String url = "http://www.settrade.com/C13_MarketSummaryIndustry.jsp",                                                         /**/                                                      
     /**/          proxy = "proxy.up.ac.th",                                                                                        /**/
     /**/          port = "8080",                                                                                                   /**/
     /**/          username = "up\\51286885",                                                                                       /**/
     /**/          password = "7489250721";                                                                                         /**/
     /**/                                                                                                                           /**/
     /**/       //URL autoIP = new URL("http://checkip.dyndns.org:8245/");                                                          /**/
     /**/       //System.out.println("Get URL");                                                                                    /**/
     /**/                                                                                                                           /**/
     /**/     URL server = new URL(url);                                                                                            /**/
     /**/                                                                                                                           /**/
     /**/       Authenticator.setDefault(new authenticator(                                                                         /**/
     /**/                   username,password));                                                                                    /**/
     /**/       Properties systemProperties = System.getProperties();                                                               /**/
     /**/       systemProperties.setProperty("http.proxyHost",proxy);                                                               /**/
     /**/       systemProperties.setProperty("http.proxyPort",port);                                                                /**/
     /**/       HttpURLConnection connection = (HttpURLConnection)server.openConnection();                                          /**/
     /**/      connection.connect();                                                                                                /**/
     /**/                                                                                                                           /**/
     /**/                                                                                                                           /**/
     /**/       try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream(),"TIS-620"))) {        /**/
     /**/                                                                                                                           /**/
     /**/                                                                                                                                /**/                                                                     /**/
     /**/                                                                                                                            /**/
     /**/           while((buffer = in.readLine()) != null) {                                                                     /**/  
     /**/                  count2++;                                                                                              /**/  
     /**/                                                                                                                           /**/  
     /**/                                                                                                                         /**/  
     /**/                  content.append(count2).append("-->").append(buffer).append("\n");                                      /**/
     /**/                                                                                                                         /**/      
      /**/            }                                                                                                           /**/  
      /**/      }                                                                                                                 /**/  
     /**/             } catch (IOException io){
            System.err.println("Couldn't create help URL: " + io);
        
               
    }   
            
            
            
          //  System.out.println(content);  
            
    
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            int countindex=0;

         
              DefaultTableModel model=new DefaultTableModel(Col1,0);
                     
                  
          String Symbol[]=new String[countindex];  
      
             String[]GroupAndCa={   "AGRO",
                                     "AGRI",
                                     "FOOD",
                                     "CONSUMP",
                                    "FASHION",
                                    "HOME",
                                    "PERSON",
                                    "FINCIAL",
                                    "BANK",
                                    "FIN",
                                    "INSUR",
                                    "INDUS",
                                    "AUTO",
                                    "IMM",
                                    "PAPER",
                                    "PETRO",
                                    "PKG",
                                    "STEEL",
                                    "PROPCON",
                                    "CONMAT",
                                    "PROP",
                                    "PFUND",
                                    "RESOURC",
                                    "ENERG",
                                    "MINE",
                                    "SERVICE",
                                    "COMM",
                                    "HELTH",
                                    "MEDIA",
                                    "PROF",
                                    "TOURISM",
                                    "TRANS",
                                    "TECH",
                                    "ETRON",
                                    "ICT"
                                    };

              
             for(int e=0;e<GroupAndCa.length;e++){
                 
               
        try {
              String[] Array= this.readFromFile(content.toString(),GroupAndCa[e]);
                // System.out.println(e+"--->>>>>>>>"+GroupAndCa[e]);
              
                    if(Array[2].startsWith("-")){
                        
                             String xc[] ={SetGroup(e)+Array[0],Array[1],Addport.Setcolor(Array[2]).toString(),Addport.Setcolor(Array[3]).toString(),Array[4],Array[5],SetGroupLink(e)+Array[0]};
                    model.addRow(xc); 
                          }else if(Array[2].startsWith("+")){
                             String xc[] ={SetGroup(e)+Array[0],Array[1],Addport.Setcolor(Array[2]).toString(),Addport.Setcolor(Array[3]).toString(),Array[4],Array[5],SetGroupLink(e)+Array[0]};
                    model.addRow(xc); 
                          }else{
                             String xc[] ={SetGroup(e)+Array[0],Array[1],Addport.Setcolor(Array[2]).toString(),Addport.Setcolor(Array[3]).toString(),Array[4],Array[5],SetGroupLink(e)+Array[0]};
                    model.addRow(xc); 
                          }
                   //String[] Col1 = {"SymbolName","Group","Open","Close","High","Low","Mk.Vol","Mk.Val('000)"};
   
                   // if(Array[6].startsWith("-")){
                    //   String xc[] ={Array[0],"",Array[2],Array[3],Array[4], Array[5],Array[10],Array[11]};
                  // model.addRow(xc); 
                     //     }else if(Array[6].startsWith("+")){
                      ///       String xc[]={Array[0],"",Array[2],Array[3],Array[4], Addport.Setcolor(Array[6]).toString(),Addport.Setcolor(Array[7]).toString(),Addport.Setcolor(Array[6]).toString(),"-",Addport.SetcolorArray(textArray).toString(),"-"};
                   //model.addRow(xc); 
                        //  }else{
                //        //      String xc[]={Array[0],"",Array[2],Array[3],Array[4], Addport.Setcolor(Array[6]).toString(),Addport.Setcolor(Array[7]).toString(),"-","-"};
                  // model.addRow(xc); 
                      //    }
                   
        }               catch (FileNotFoundException ex) {
                            ex.printStackTrace();
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        } catch (SQLException ex) {
        }
             }
              
              
              
            
              
          
            

          JT.setModel(model);
         JT.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
         JT.getColumnModel().getColumn(0).setResizable(false);
         JT.getColumnModel().getColumn(0).setPreferredWidth(200);
         JT.getColumnModel().getColumn(1).setResizable(false);
         JT.getColumnModel().getColumn(1).setPreferredWidth(200);
         JT.getColumnModel().getColumn(2).setResizable(false);
         JT.getColumnModel().getColumn(2).setPreferredWidth(200);
         JT.getColumnModel().getColumn(3).setResizable(false);
         JT.getColumnModel().getColumn(3).setPreferredWidth(200);
         JT.getColumnModel().getColumn(4).setResizable(true);
         JT.getColumnModel().getColumn(4).setPreferredWidth(200);
         JT.getColumnModel().getColumn(6).setResizable(true);
         JT.getColumnModel().getColumn(6).setPreferredWidth(0);
           JT.getColumnModel().getColumn(5).setResizable(true);
         JT.getColumnModel().getColumn(5).setPreferredWidth(200);
                DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();
      
dtcr.setHorizontalAlignment(SwingConstants.RIGHT);
JT.getColumn("ล่าสุด").setCellRenderer(dtcr);
JT.getColumn("เปลี่ยนแปลง").setCellRenderer(dtcr);
JT.getColumn("%เปลี่ยนแปลง").setCellRenderer(dtcr);
JT.getColumn("ปริมาณ(หุ้น)").setCellRenderer(dtcr);
JT.getColumn("มูลค่า '000 บาท").setCellRenderer(dtcr);
             
            try {
                Thread.sleep(5000);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
    }
           
        }
            
       
    String[] Col1 = {"กลุ่ม/หมวดอุตสาหกรรม","ล่าสุด","เปลี่ยนแปลง","%เปลี่ยนแปลง","ปริมาณ(หุ้น)","มูลค่า '000 บาท","LINK"};
       String symbolx;int Block[]=new int[12];
public static String[]sym=new String[12];String x ="";
     


public String SetGroup(int indexG){
    String SetG="";
    if(indexG==0||indexG==3||indexG==7||indexG==11||indexG==18||indexG==22||indexG==25||indexG==32){
        
        SetG="";
    }else{
         SetG="-";
    }
    
    return SetG;
}


public String SetGroupLink(int indexG){
    String SetG="";
    if(indexG==0||indexG==3||indexG==7||indexG==11||indexG==18||indexG==22||indexG==25||indexG==32){
        
        SetG="http://www.settrade.com/C13_MarketSummary.jsp?detail=INDUSTRY&industry=";
    }else{
         SetG="http://www.settrade.com/C13_MarketSummary.jsp?detail=INDUSTRY&sector=";
    }
    
    return SetG;
}
     
public String[] readFromFile(String textString,String symbol) throws SQLException, FileNotFoundException, IOException {

        
        BufferedReader bufferedReader = null;
        String xc ="";
        String ya;
       for(int i=0;i<sym.length;i++){
           sym[i]=null;
       }
        
        try {
            //Construct the BufferedReader object
            bufferedReader = new BufferedReader(new StringReader(textString));
            String line = null;
          
            while ((line = bufferedReader.readLine()) != null) {
                  int count3 = 0;int b[] = new int[1000];int c[] =new int[1000];int count2 = 0;
                //System.out.println(line.toString());
                            String linenumber = "";
                            String y ="";
                    if(line.contains("/C13_MarketSummary.jsp?detail=INDUSTRY&industry="+symbol+"\"")||line.contains("/C13_MarketSummary.jsp?detail=INDUSTRY&sector="+symbol+"\"")){
                            for(int i=0;i<5;i++){
                                char xx=line.charAt(i);
                                
                                if(xx>='0'&&xx<='9'){
                                    linenumber+=line.substring(i, i+1);       
                                }        
                            } //System.out.print(linenumber);  //System.out.println(aa);
                       if(line.startsWith(linenumber)){
                        for(int i=0;i<line.length();i++){
                       char a= line.charAt(i);
                      if(a=='>'){
                          ++count3;
                         b[count3]=i;
                      }else if(a=='<'){
                           ++count2;
                          c[count2]=i;
                      }}
                   for(int i=0;i<100;i++){
                          if(b[i]!=0&&c[i]!=0){
                             if(AlertRunnable.isDigit(line.substring(b[i]+1,c[i]))){
                                 ya=line.substring(b[i]+1,c[i]);
                                 if(ya.equals(symbol)){
                                     sym[0]=ya;
                                     
                                 }
                             }
                        } }
                  //  x+="@"+y;
                    

                    for(int k=1;k<=11;k++){
                        Block[k]=Integer.parseInt(linenumber)+k;
                        
                        
                        
                    }
                     }
                    // System.out.println(Block[1]);
                   
                            ////////////////////////////////////
                    // System.out.print(""+y);
                     } 
                        for(int k=1;k<=11;k++){
                       
                        if(line.startsWith(String.valueOf(Block[k])+"-->")){
                        for(int i=0;i<line.length();i++){
                       char a= line.charAt(i);
                      if(a=='>'){
                          ++count3;
                         b[count3]=i;
                      }else if(a=='<'){
                           ++count2;
                          c[count2]=i;
                      }}
                   for(int i=0;i<100;i++){
                          if(b[i]!=0&&c[i]!=0){
                             if(AlertRunnable.isDigit(line.substring(b[i]+1,c[i]))){              
                                      xc=line.substring(b[i]+1,c[i]);
                                      
                             }}
                             }

                       
                           y=xc;
                           
                       
                        
                    //x+="@"+y;
                  sym[k]=y;
                   if(k==11){
                    break;

                           }
                     //  System.out.print(y);
                     }
                        
                   
                    }
                 
 
        
            
         
            }
        }
 catch (FileNotFoundException ex) {
            Logger.getLogger(AlertRunnable.class.getName()).log(Level.SEVERE, null, ex);
        }         catch (IOException ex) {
        } finally {
            //Close the BufferedReader
            try {
                if (bufferedReader != null)
                    bufferedReader.close();
            } catch (IOException ex) {
            }
        }
        return sym;
    }
     
}
